#include "userConfig.h"
#include "../driver/usart.h"
#include "task.h"
#include "mm.h"
#include "queue.h"
#include "mutex.h"
#include "semaphore.h"
#include "signal.h"
#include "stm32f4xx.h"

//任务优先级
#define START_TASK_PRIO		2
//任务堆栈大小
#define START_STK_SIZE 		128
//任务句柄

#define TASK_DELAY      100

struct task_struct * StartTask_Handler;
struct task_struct * Task_Handler7;
struct task_struct * Task_Handler8;
struct task_struct * Task_Handler9;
struct task_struct * Task_Handler10;

void task7_task(void *args);
void task8_task(void *args);
void task9_task(void *args);
void task10_task(void *args);

//任务优先级
#define TASK_TASK_LOW_PRIO		2
#define TASK_TASK_MID_PRIO		3
#define TASK_TASK_HIGH_PRIO     4
//任务堆栈大小
#define TASK_STK_SIZE 		128
//任务句柄
struct msg_queue * big_date_queue;   /* 大数据句柄 */
struct mutex * mutex_handle;   /* 大数据句柄 */
struct semaphore *semphore_handle;   /* 大数据句柄 */

struct signal *g_signal;
TIM_HandleTypeDef TIM_InitStruct;

//定时器3中断服务程序
void TIM3_IRQHandler(void)
{
    uint32_t   err = 0;
    uint32_t send = 0x55aa;

    HAL_TIM_IRQHandler(&TIM_InitStruct);
    // err = semaphore_up(semphore_handle);
    // if(err != 1) {
    //     usart_transmit("信号量发送失败\r\n");
    // }
    // usart_transmit("TIM3_IRQHandler\r\n");
    signal_send(g_signal);
}

void TIM3_Int_Init(uint16_t arr, uint16_t psc)
{
   /* Select Timer-2 for further configuration */
    TIM_InitStruct.Instance = TIM3;
    TIM_InitStruct.Init.Prescaler   = 1000;
     /* Up-Counter mode*/
    TIM_InitStruct.Init.CounterMode = TIM_COUNTERMODE_UP;
    TIM_InitStruct.Init.Period = 10000;
    /*
        Finally initialize Timer-2
    */
    while (HAL_TIM_Base_Init(&TIM_InitStruct)!= HAL_OK);

    /*
        Enable timer-2 IRQ interrupt
    */
    HAL_TIM_Base_Start_IT(&TIM_InitStruct);

    /* Enable interrupt at IRQ-Level */
    HAL_NVIC_EnableIRQ(TIM3_IRQn);

    /*
        Start the timer
    */
    HAL_TIM_Base_Start(&TIM_InitStruct);
}

/* 任务一，实现入队 */
static void task1_task(void *pvParameters)
{
    uint32_t send = 0;
    uint32_t   err = 0;

    while(1)
    {
        err = queue_write(big_date_queue, &send);
        if(err != 1)
        {
            usart_transmit("queue队列发送失败\r\n");
        } else {
            send++;
            if (send == 99999)
                send = 0;
            // usart_transmit("queue队列发送成功: %d\r\n", count++);
        }
        task_delay(100);
    }
}

/* 任务二，小数据出队 */
static void task2_task(void *pvParameters)
{
    uint32_t recv_count;
    uint32_t err = 0;

    while(1)
    {
        // task_delay(100);
        // usart_transmit("task2 running\r\n");
        err = queue_read(big_date_queue, &recv_count);
        if(err != 1)
        {
            usart_transmit("big_date_queue队列读取失败\r\n");
        }else
        {
            usart_transmit("数据: %x\n", recv_count);
        }
    }
}

void task3_task(void *args)
{
	while(1)
	{
        usart_transmit("high任务尝试获取信号量\r\n");
        task_delay(TASK_DELAY);
        mutex_lock(mutex_handle);
        usart_transmit("high任务成功获取信号量\r\n");
        usart_transmit("high任务释放信号量\n");
        mutex_unlock(mutex_handle);

        task_delay(TASK_DELAY);
	}
}

void task4_task(void *args)
{
	while(1)
	{
        usart_transmit("low 任务尝试获取信号量\r\n");
        task_delay(TASK_DELAY);
        mutex_lock(mutex_handle);
        usart_transmit("low 任务成功获取信号量\r\n");
        usart_transmit("low 任务释放信号量\r\n");
        mutex_unlock(mutex_handle);

        task_delay(TASK_DELAY);
	}
}

void task5_task(void *args)
{
	uint32_t task5_num=0;

	while(1)
	{
		task5_num++;
		usart_transmit("任务 5 已经执行:%d次\r\n",task5_num);
        task_delay(2*TASK_DELAY);
	}
}

void task6_task(void *args)
{
	uint32_t task6_num=0;

	while(1)
	{
		task6_num++;
		usart_transmit("任务 6 已经执行:%d次\r\n",task6_num);
        task_delay(TASK_DELAY);
	}
}



void task8_task(void *args)
{
	uint32_t task8_num=0;

	while(1)
	{
        // usart_transmit("任务 8 任务尝试获取信号量\r\n");
        // task_delay(TASK_DELAY);
        semaphore_down(semphore_handle);
        usart_transmit("任务 8 任务成功获取信号量\r\n");
        // usart_transmit("任务 8 任务释放信号量\r\n");

        // task_delay(TASK_DELAY);
	}
}

void task9_task(void *args)
{
	uint32_t task9_num=0;

	while(1)
	{
        usart_transmit("任务 9 任务尝试获取信号量\r\n");
        task_delay(TASK_DELAY);
        semaphore_down(semphore_handle);
        usart_transmit("任务 9 任务成功获取信号量\r\n");
        usart_transmit("任务 9 任务释放信号量\r\n");
        semaphore_up(semphore_handle);

        task_delay(TASK_DELAY);
	}
}

void task10_task(void *args)
{
	uint32_t task10_num=0;

	while(1)
	{
		task10_num++;
		usart_transmit("任务10已经执行:%d次\r\n",task10_num);
        task_delay(TASK_DELAY);
	}
}

void task11_task(void *args)
{
	uint32_t task11_num=0;

	while(1)
	{
		task11_num++;
		usart_transmit("任务11已经执行:%d次\r\n",task11_num);
        task_delay(TASK_DELAY);
	}
}

void task12_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务12已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task13_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务13已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task14_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务14已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task15_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务15已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task16_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务16已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task17_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务17已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task18_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务18已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task19_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务19已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task20_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务20已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task21_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务21已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task22_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务22已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task23_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务23已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task24_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务24已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task25_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务25已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task26_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务26已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task27_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务27已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task28_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务28已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task29_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务29已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task30_task(void *args)
{
	uint32_t task12_num=0;

	while(1)
	{
		task12_num++;
		usart_transmit("任务30已经执行:%d次\r\n",task12_num);
        task_delay(TASK_DELAY);
	}
}

void task7_task(void *args)
{
    task_create(task5_task,
                "task5_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_MID_PRIO);

    task_create(task6_task,
                "task6_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_HIGH_PRIO);

    Task_Handler8 = task_create(task8_task,
                "task8_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_LOW_PRIO);

    // Task_Handler9 = task_create(task9_task,
    //             "task9_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // Task_Handler10 = task_create(task10_task,
    //             "task10_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task11_task,
    //             "task11_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task12_task,
    //             "task11_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task13_task,
    //             "task11_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task14_task,
    //             "task11_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task15_task,
    //             "task15_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task16_task,
    //             "task16_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task17_task,
    //             "task17_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task18_task,
    //             "task18_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);
    // task_create(task19_task,
    //             "task19_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task20_task,
    //             "task20_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task21_task,
    //             "task21_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task22_task,
    //             "task22_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task23_task,
    //             "task23_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task24_task,
    //             "task24_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task25_task,
    //             "task25_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task26_task,
    //             "task26_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);
    // task_create(task27_task,
    //             "task27_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task28_task,
    //             "task28_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task29_task,
    //             "task29_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task30_task,
    //             "task30_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    task_delete(Task_Handler7);
}

//开始任务任务函数
void start_task(void *args)
{
    disable_interrupt();

    // task_create(task1_task,
    //             "task1_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);


    // task_create(task2_task,
    //             "task2_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )4);

    // task_create(task3_task,
    //             "task3_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )2);

    // task_create(task4_task,
    //             "task4_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    Task_Handler7 = task_create(task7_task,
                "task7_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task9_task,
    //             "task9_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task10_task,
    //             "task10_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_HIGH_PRIO);

    TIM3_Int_Init(5000-1, 8400-1);
    task_delete(StartTask_Handler);
    enable_interrupt();
}

void signal_action(void *ptr)
{
    usart_transmit("处理接收到的信号\r\n");
}



void task_demo(void)
{
    /* 队列的创建 */
    big_date_queue = queue_create(100, sizeof(uint32_t));
    if(big_date_queue != NULL)
    {
        usart_transmit("big_date_queue队列创建成功\r\n");
    }
    else
    {
        usart_transmit("big_date_queue队列创建失败\r\n");
    }

    mutex_handle = mutex_create();
    if(mutex_handle != NULL)
    {
        usart_transmit("互斥量创建成功！！！\r\n");
    }
    mutex_unlock(mutex_handle);

    semphore_handle = semaphore_create();
    if(semphore_handle != NULL)
    {
        usart_transmit("信号量创建成功！！！\r\n");
    }



    g_signal = signal_create("test", signal_action, NULL);
    if(g_signal != NULL)
    {
        usart_transmit("信号创建成功！！！\r\n");
    }

    //创建开始任务
    StartTask_Handler = task_create(start_task,
                "start_task",
                (uint16_t       )START_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )START_TASK_PRIO);

}




